package com.example.com.skills_utf8;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.media.Image;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.app.BaseActivity;
import com.app.net.NetHelper;
import com.app.umeng.Umeng;
import com.app.util.Utils;
import com.bean.Talk;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.entity.UMessage;
import com.utils.GsonParse;
import com.utils.g;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class _62Activity extends BaseActivity implements _62Fragment.OnFragmentInteractionListener{



    _62Activity activity = this;




    ListView listview_drawer;
    Adapter_drawer adapter_drawer = new Adapter_drawer();
    List<String> listdata_drawer = new ArrayList<String>();

    DrawerLayout drawerlayout ;


    ImageView iv_left;
    ImageView circle;


    TextView tv_search;


    int index = 0;




    @Override
    public Integer InitLayout() {
        return R.layout.activity__62;
    }

    @Override
    public void InitView() {

        tv_search = (TextView)findViewById(R.id.tv_search);

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }



    @Override
    public void InitListener() {
        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(activity, _62ActivitySearchQrcodeLandspace.class);
                startActivity(intent);

            }
        });

    }

    @Override
    public void InitProcess() {


        drawerlayout = (DrawerLayout)findViewById(R.id.drawerlayout);


        init_title();
        init_main();
        init_drawerlayout();

    }

    void init_main(){

        Fragment _62 = (Fragment)new _62Fragment();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.layout_content, _62);
        transaction.commit();



    }


    void drawlayout_control(){

        ///*
        if (drawerlayout.isDrawerVisible(GravityCompat.START)) {
            drawerlayout.closeDrawer(GravityCompat.START);//关闭抽屉
        } else {

            adapter_drawer.clear();
            adapter_drawer.init();

            drawerlayout.openDrawer(GravityCompat.START);//打开抽屉
        }
        //*/

    }


    void init_title(){

        iv_left  = (ImageView)findViewById(R.id.iv_left);

        iv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                drawlayout_control();

                //Animation animation = AnimationUtils.loadAnimation(activity, R.anim.scale_circle);
                //iv_left.startAnimation(animation);

                // 按钮按下，将抽屉打开
                //drawerlayout.openDrawer(Gravity.LEFT);


                /*  ok
                // 圆形向外扩散
                Animation animation = AnimationUtils.loadAnimation(activity, R.anim.scale_circle);
                animation.setDuration(1500);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        circle.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        circle.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                circle.startAnimation(animation);
                */


                // 后天按钮旋转
                Animation animation_rota = AnimationUtils.loadAnimation(activity, R.anim.rotate);
                animation_rota.setFillAfter(true);
                iv_left.startAnimation(animation_rota);
                //iv_left.setRotation(90);

                mScaleCircle.addTo(activity, iv_left);
                mScaleCircle.startAnimation();



            }
        });



        /*
        // 圆形向外扩散
        FrameLayout layout_title = (FrameLayout)findViewById(R.id.layout_title);
        //com.app.view.CircleSurfaceView circle = new com.app.view.CircleSurfaceView(this);
        //circle.SetPointCenter(new Point(200 / 2, 50));
        circle = new ImageView(activity);
        circle.setImageResource(R.drawable.shape_circle);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(120, 120);
        layout_title.addView(circle, params);

        circle.setVisibility(View.GONE);
        */






    }

    ScaleCircle mScaleCircle = new ScaleCircle();



    class ScaleCircle
    {
        ImageView iv_circle;

        public ScaleCircle(){

        }

        void addTo(Context context, final View view){

            if(iv_circle == null) {
                iv_circle = new ImageView(context);
                iv_circle.setImageResource(R.drawable.shape_circle);
                iv_circle.setVisibility(View.GONE);

                int width = view.getMeasuredWidth();
                int height = view.getMeasuredHeight();

                FrameLayout layout_view = (FrameLayout) view.getParent();
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);//(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                layout_view.addView(iv_circle, params);

            }


            /*
            ViewTreeObserver ob = view.getViewTreeObserver();
            ob.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { // java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

                @Override
                public boolean onPreDraw() {
                    // TODO Auto-generated method stub

                    int width = view.getMeasuredWidth();
                    int height = view.getMeasuredHeight();

                    FrameLayout layout_view = (FrameLayout) view.getParent();
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
                    layout_view.addView(iv_circle, params);

                    return true;
                }
            });
            */


        }

        void startAnimation(){
            Animation animation = AnimationUtils.loadAnimation(activity, R.anim.scale_circle);
            animation.setDuration(1500);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    iv_circle.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    iv_circle.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            iv_circle.startAnimation(animation);


        }


    }



    void init_drawerlayout(){

        adapter_drawer.init();


        listview_drawer = (ListView)findViewById(R.id.left_drawer);
        listview_drawer.setAdapter(adapter_drawer);

        listview_drawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //TextView tv_nickname = (TextView)view.findViewById(R.id.tv_nickname);

                drawlayout_control();

                index = position;
                adapter_drawer.notifyDataSetChanged();
                //listview_drawer.setItemChecked(position, true);


            }
        });


        drawerlayout = (DrawerLayout)findViewById(R.id.drawerlayout);
        drawerlayout.setScrimColor(0xa0000000);
        //drawerlayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);//设置shadow

        drawerlayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View view, float v) {

            }

            @Override
            public void onDrawerOpened(View view) {

            }

            @Override
            public void onDrawerClosed(View view) {

            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });






    }






    @Override
    public void onFragmentInteraction(Uri uri) {

    }






    class Adapter_drawer extends BaseAdapter
    {

        public void init(){

            listdata_drawer.add("听音乐");
            listdata_drawer.add("看电影");
            listdata_drawer.add("打篮球");
            listdata_drawer.add("踢足球");

            this.notifyDataSetChanged();
        }

        public void clear(){

            listdata_drawer.clear();
        }


        @Override
        public int getCount() {
            return listdata_drawer.size();
        }

        @Override
        public Object getItem(int position) {
            return listdata_drawer.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.layout_62_drawerlayout, null);

            }

            String content = listdata_drawer.get(position);

            LinearLayout layout_content = (LinearLayout)convertView.findViewById(R.id.layout_content);
            ImageView iv_logo = (ImageView)convertView.findViewById(R.id.iv_logo);
            TextView tv_content = (TextView)convertView.findViewById(R.id.tv_content);
            TextView tv_num = (TextView)convertView.findViewById(R.id.tv_num);


            int drawable = 0;
            if(position == 0){
                drawable = R.drawable.drawerlayout_music;
            }
            else if(position == 1){
                drawable = R.drawable.drawerlayout_movie;
            }
            else if(position == 2){
                drawable = R.drawable.drawerlayout_basketball;
            }
            else{
                drawable = R.drawable.drawerlayout_football;
            }
            iv_logo.setImageResource(drawable);
            tv_content.setText(content);
            tv_num.setText(""+position);



            //这样各项就到达就参差不齐
            {
                time = 300 + position * 200;
            }

            Animation animation = AnimationUtils.loadAnimation(activity, android.R.anim.slide_in_left);
            animation.setDuration(time);
            layout_content.startAnimation(animation);

            if(position == index){
                //img设置成黄色， 使选择的东西统一色调，好思路；
                tv_content.setTextColor(getResources().getColor(R.color.yellow_activity_62));
                tv_num.setTextColor(getResources().getColor(R.color.yellow_activity_62));
            }
            else{
                tv_content.setTextColor(getResources().getColor(android.R.color.white));
                tv_num.setTextColor(getResources().getColor(android.R.color.white));
            }


            return convertView;
        }

        long time = 0;

    }


}
